package com.wechat.controller;

import com.wechat.apidoc.WxRecordInterface;
import com.wechat.entity.Question;
import com.wechat.entity.QuestionDetail;
import com.wechat.entity.QuestionLib;
import com.wechat.entity.User;
import com.wechat.entity.extend.AnswerDetailExt;
import com.wechat.entity.extend.AnswerExt;
import com.wechat.entity.extend.TestRecord;
import com.wechat.service.QuestionService;
import com.wechat.service.RecordService;
import com.wechat.service.UserService;
import com.wechat.utils.ActionResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by ycx on 2018/4/13.
 */
@RestController
@RequestMapping(value = "api/v1")
public class WxRecordController implements WxRecordInterface {

    private static Logger logger = LoggerFactory.getLogger(WxRecordController.class);

    @Autowired
    private UserService userService;
    @Autowired
    private RecordService recordService;
    @Autowired
    private QuestionService questionService;

    /**
     * 查看题目列表
     *
     * @param questionId
     * @param key
     * @return
     */
    @Override
    @RequestMapping(value = "record/sel_question_list", method = RequestMethod.POST)
    public ActionResult selQuestionList(Integer questionId, String key) {
        logger.info("[查看题目列表接口]:{}", "key=" + key + ",questionId=" + questionId);

        ActionResult result = new ActionResult<>();

        User user = userService.getLoginUser(key);
        if (user == null) {
            result.setLoginTimeout(true);
            result.setMessage("用户登录超时");
            return result;
        }

        if (questionId == null || questionId == 0) {
            result.setSuccess(false);
            result.setMessage("参数不正确");
            return result;
        }

        try {
            // 查询它主要是为了选择性展示下面的按钮功能
            Question question = questionService.findQuestion(questionId);
            List<QuestionLib> questionLibList = recordService.findQuestionLibList(questionId);

            result.put("question", question);
            result.setData(questionLibList);
            result.setMessage("查看题目列表查询成功");
            result.setSuccess(true);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("查看题目列表查询失败");

            logger.warn("[查看题目列表接口异常]：", e);
        }

        return result;
    }

    /**
     * 使用当前题目再来一发
     *
     * @param questionId
     * @param key
     * @return
     */
    @Override
    @RequestMapping(value = "record/use_current_question", method = RequestMethod.POST)
    public ActionResult useCurrentQuestion(Integer questionId, String key) {
        logger.info("[使用当前题目再来一发接口]:{}", "key=" + key + ",questionId=" + questionId);

        ActionResult result = new ActionResult<>();

        User user = userService.getLoginUser(key);
        if (user == null) {
            result.setLoginTimeout(true);
            result.setMessage("用户登录超时");
            return result;
        }

        if (questionId == null || questionId == 0) {
            result.setSuccess(false);
            result.setMessage("参数不正确");
            return result;
        }

        try {

            Question question = questionService.findQuestion(questionId);

            QuestionDetail questionDetail = new QuestionDetail();
            questionDetail.setQuestionId(questionId);
            List<QuestionDetail> questionDetailList = recordService.selQuestionDetailsByParam(questionDetail);

            Integer userId = user.getId();
            Question record = new Question();
            record.setUserId(userId);
            record.setTotalRight(question.getTotalRight());
            record.setTotalQuestionNumber(question.getTotalQuestionNumber());
            record.setDataState((short) 1);

            Integer newId = recordService.batchSaveQuestionDetail(questionDetailList, record);

            result.setMessage("使用当前题目再来一发成功");
            result.setData(newId);
            result.setSuccess(true);
        } catch (Exception e) {
            logger.warn("使用当前题目再来一发接口异常：", e);

            result.setMessage("使用当前题目再来一发失败");
            result.setSuccess(false);
        }

        return result;
    }

    /**
     * 单个好友的答题记录
     *
     * @param userId
     * @param key
     * @return
     */
    @Override
    @RequestMapping(value = "record/sel_answer_detail_ext", method = RequestMethod.POST)
    public ActionResult selAnswerDetailExt(Integer answerId, Integer userId, String key) {
        logger.info("[单个好友的答题记录接口]:{}", "key=" + key + ",answerId=" + answerId + ",userId=" + userId);

        ActionResult result = new ActionResult<>();

        User loginUser = userService.getLoginUser(key);
        if (loginUser == null) {
            result.setLoginTimeout(true);
            result.setMessage("用户登录超时");
            return result;
        }

        if (answerId == null || answerId == 0) {
            result.setSuccess(false);
            result.setMessage("参数不正确");
            return result;
        }

        try {
            List<AnswerDetailExt> answerDetailExtList = recordService.findAnswerDetailExt(answerId);
            User user = userService.findById(userId);

            result.setMessage("单个好友的答题记录查询成功");
            result.put("user", user);
            result.setData(answerDetailExtList);
            result.setSuccess(true);
        } catch (Exception e) {
            logger.warn("[单个好友的答题记录接口异常]：", e);

            result.setMessage("单个好友的答题记录查询失败");
            result.setSuccess(false);
        }

        return result;
    }

    /**
     * 我发出的 页面展示
     *
     * @param key
     * @return
     */
    @Override
    @RequestMapping(value = "record/show_i_send", method = RequestMethod.POST)
    public ActionResult showISend(String key) {
        logger.info("[我发出的 页面展示接口]:{}", "key=" + key);

        ActionResult result = new ActionResult<>();

        User user = userService.getLoginUser(key);
        if (user == null) {
            result.setLoginTimeout(true);
            result.setMessage("用户登录超时");
            return result;
        }

        try {
            Integer userId = user.getId();
            double totalAmount = recordService.sendSumRedPacketByUserId(userId);
            int totalNumber = recordService.sendCountRedPacketByUserId(userId);
            List<TestRecord> testRecordList = recordService.selTestRecord(userId,1);

            result.setData(testRecordList);
            result.put("totalNumber", totalNumber);
            result.put("totalAmount", totalAmount);
            result.setMessage("我发出的 页面展示数据查询成功");
            result.setSuccess(true);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("我发出的 页面展示数据查询失败");

            logger.warn("[我发出的 页面展示接口异常]：", e);
        }

        return result;
    }

    /**
     * 我收到的 展示页面
     *
     * @param key
     * @return
     */
    @Override
    @RequestMapping(value = "record/show_i_received", method = RequestMethod.POST)
    public ActionResult showIReceived(String key) {
        logger.info("[获取答题记录详情接口]:{}", "key=" + key);

        ActionResult result = new ActionResult<>();

        User user = userService.getLoginUser(key);
        if (user == null) {
            result.setLoginTimeout(true);
            result.setMessage("用户登录超时");
            return result;
        }

        try {
            Integer userId = user.getId();
            List<AnswerExt> answerExtList = recordService.selAnswerRecordsByUserId(userId);
            double totalAmount = recordService.getSumRedPacketByUserId(userId);
            int totalNumber = recordService.getCountRedPacketByUserId(userId);

            result.setSuccess(true);
            result.setMessage("我收到的数据查询成功");
            result.setData(answerExtList);
            result.put("totalAmount", totalAmount);
            result.put("totalNumber", totalNumber);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("我收到的数据查询失败");

            logger.warn("[我收到的 展示页面接口异常]：", e);
        }

        return result;
    }

}




















































